package com.aegean.MonitorExamSYS.center.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.aegean.MonitorExamSYS.center.component.SelectConductionInfo;
import com.aegean.MonitorExaxSYS.model.ExamPosition;
import com.opensymphony.xwork2.ActionSupport;

@Controller("AutoArrangeForPosition")
@Scope("prototype")
public class AutoArrangeForPosition extends ActionSupport implements
		ServletRequestAware {
	@Resource
	CenterBusiness business;
	private HttpServletRequest request;
	private HttpSession session;
	private Map<Integer, List<Integer>> con_PositionsMap;
	private Map<Integer, Integer> con_PositionCountMap;
	private Date startTime;
	private Date endTime;
	private LinkedList<SelectConductionInfo> conductions;
	private boolean success;
	private String msg;

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		con_PositionsMap = (Map<Integer, List<Integer>>) session
				.getAttribute("confirmConductionsMap");
		conductions = (LinkedList<SelectConductionInfo>) session
				.getAttribute("autoArrangeConductionPersonCount");//获取自动安排考场之后的map对象
		con_PositionCountMap = new HashMap<Integer, Integer>();// 考点考生数量分配map
		Object[] positionsId = con_PositionsMap.keySet().toArray();
		Map<Integer, List<ExamPosition>> maps = business.showPositionLists(
				positionsId, startTime, endTime);// 读取Map类型的考点信息
		for (Object obj : maps.keySet().toArray()) {
			int id = Integer.parseInt(obj.toString().trim());
			int personCount = 0;
			List<ExamPosition> list = maps.get(id);
			Collections.sort(list, new sortCollection());
			int i = 0;
			while (i < conductions.size()) {
				if (conductions.get(i).getId() == id) {
					personCount = conductions.get(i).getPersonCount();
					break;
				}
				i++;
			}
			con_PositionCountMap.put(id, personCount);// 考点考生数量分配map添加
			//System.out.println("是否家中"+con_PositionsMap.containsKey(id));
			List<Integer> li = con_PositionsMap.get(id);
			if (li == null) {
				li = new ArrayList<Integer>();
			}
			for (ExamPosition one : list) {
				if (personCount <= one.getExamPosition_PersonCount()) {// 如果规定考试数量小于考场容量，证明已经该考场已经停止
					li.add(one.getExamPosition_Id());
					personCount = 0;
					break;
				} else {
					li.add(one.getExamPosition_Id());
					personCount -= one.getExamPosition_PersonCount();
					if (personCount == 0) {
						break;
					}
				}
			}
			
			con_PositionsMap.put(id, li);
			conductions.remove(i);
		}
		session.removeAttribute("confirmConductionsMap");
		//System.out.println("AutoArrageForConductionAction:"+con_PositionsMap.size());
		session.setAttribute("confirmConductionsMap", con_PositionsMap);//存储考点考场的分配
		session.setAttribute("confirmPositionPersonCount", con_PositionCountMap);//存储考点考生数量的分配
		msg = "自动分配考场成功！";
		success = true;
		return SUCCESS;
	}

	class sortCollection implements Comparator {
		@Override
		public int compare(Object o1, Object o2) {
			// TODO Auto-generated method
			int result;
			ExamPosition one = (ExamPosition) o1;
			ExamPosition two = (ExamPosition) o2;
			result = one.getExamPosition_PersonCount() < two
					.getExamPosition_PersonCount() ? 1 : (one
					.getExamPosition_PersonCount() == two
					.getExamPosition_PersonCount() ? 0 : -1);
			return result;
		}
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		session = request.getSession();

	}

}
